﻿using KPIS.GERP.MVC.Classes;
using KPIS.GERP.SERVICE.Model;
using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;

namespace KPIS.GERP.MVC.Controllers
{
    public class CommandAddController : Controller
    {
        GERPContext db = new GERPContext();
        Common common = new Common();

        //
        // GET: /CommandPenalty/

        public ActionResult Index()
        {
            return View(db.hrm_ps_commands
                .Where(m => m.hrm_ps_command_types.cmd_group_seq == 1 && m.record_status != "D")
                .OrderByDescending(m => m.created_when).ThenBy(m => m.cmd_seq).ThenBy(m => m.cmd_type_seq)
                .ToList());
        }

        //
        // GET: /CommandPenalty/Details/5

        public ActionResult Details(int id)
        {
            hrm_ps_commands item = db.hrm_ps_commands.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }

            //item.approved_date = common.CurrectDateTime(item.approved_date);
            return View(item);
        }

        //
        // GET: /CommandPenalty/Create

        public ActionResult Create()
        {
            return View(new hrm_ps_commands());
        }

        //
        // POST: /CommandPenalty/Create

        [HttpPost]
        // [ValidateAntiForgeryToken]
        public ActionResult Create(hrm_ps_commands model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    SsMng ssmng = new SsMng();
                    model.created_by = ssmng.EmpSeqI;
                    model.created_when = DateTime.Now;

                    //using (GERPContext db = new GERPContext())
                    //{
                    db.hrm_ps_commands.Add(model);
                    db.SaveChanges();
                    //}
                    return RedirectToAction("Edit", new { id = model.cmd_seq });
                }

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //public ActionResult CreateDetail()
        //{
        //    using (GERPContext db = new GERPContext())
        //    {
        //        //hrm_ps_commands item = db.hrm_ps_commands.Find(id);

        //        return View();
        //    }
        //}

        public ActionResult CreateDetail(int id)
        {
            var model = new hrm_ps_command_dtls();
            model.cmd_seq = id;
            if (model == null)
            {
                return HttpNotFound();
            }
            model.hrm_ps_commands = db.hrm_ps_commands.Find(id);

            return View("CreateDetail", model);
        }

        //POST: /CommandPenalty/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateDetail(hrm_ps_command_dtls model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //if (model.end_date != null && model.start_date > model.end_date)
                    //{
                    //    ModelState.AddModelError("", "มีผลตั้งแต่วันที่ ต้องไม่มากกว่า มีผลจนถึงวันที่");
                    //    return Edit(model.cmd_seq.Value);
                    //}

                    using (GERPContext db = new GERPContext())
                    {
                        SsMng ssmng = new SsMng();
                        //model.start_date = Common.GetDate(model.start_date);
                        //model.end_date = Common.GetDate(model.end_date);

                        model.record_status = "W";
                        model.created_by = ssmng.EmpSeqI;
                        model.created_when = DateTime.Now;

                        db.hrm_ps_command_dtls.Add(model);
                        db.SaveChanges();
                    }
                    return RedirectToAction("Edit", "CommandAdd", new { id = model.cmd_seq });
                }

                return View(model);
            }
            catch
            {
                return View(model);
            }
        }


        //
        // GET: /CommandPenalty/Edit/5
        public ActionResult Edit(int id = 0)
        {
            //using (GERPContext db = new GERPContext())
            //{
            hrm_ps_commands item = db.hrm_ps_commands.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }

            //item.approved_date = common.CurrectDateTime(item.approved_date);
            return View(item);
            //}
        }

        //
        // POST: /CommandPenalty/Edit/5

        [HttpPost]
        // [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, hrm_ps_commands model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    SsMng ssmng = new SsMng();
                    model.updated_by = ssmng.EmpSeqI;
                    model.updated_when = DateTime.Now;

                    //using (GERPContext db = new GERPContext())
                    //{
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChanges();
                    //}
                    return RedirectToAction("Edit", new { id = model.cmd_seq });
                }

                return Edit(model.cmd_seq);
            }
            catch
            {
                return Edit(model.cmd_seq);
            }
        }

        public ActionResult EditDetail(int id = 0)
        {
            var model = db.hrm_ps_command_dtls.Find(id);
            if (model == null)
            {
                return HttpNotFound();
            }
            model.hrm_ps_commands = db.hrm_ps_commands.Find(model.cmd_seq);

            return View(model);
        }

        [HttpPost]
        // [ValidateAntiForgeryToken]
        public ActionResult EditDetail(int id, hrm_ps_command_dtls model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    SsMng ssmng = new SsMng();
                    model.updated_by = ssmng.EmpSeqI;
                    model.updated_when = DateTime.Now;

                    //using (GERPContext db = new GERPContext())
                    //{
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChanges();
                    //}
                    return RedirectToAction("Edit", new { id = model.cmd_seq });
                }

                return Edit(model.cmd_seq.Value);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "มีความผิดพลาดในการบันทึก [" + ex + "]");
                return View(model);
            }
        }

        //
        // GET: /CommandPenalty/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        //
        // POST: /CommandPenalty/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                hrm_ps_commands command = db.hrm_ps_commands.Find(id);
                command.updated_by = new SsMng().EmpSeqI;
                command.updated_when = DateTime.Now;
                command.record_status = "D";
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return Index();
            }
        }

        //public ActionResult Delete(int id)
        //{
        //    try
        //    {
        //        hrm_ps_commands command = db.hrm_ps_commands.Find(id);
        //        command.updated_by = new SsMng().EmpSeqI;
        //        command.updated_when = DateTime.Now;
        //        command.record_status = "D";
        //        db.SaveChanges();

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return Index();
        //    }
        //}

        public ActionResult DetailDelete(int id, hrm_ps_command_dtls model)
        {
            try
            {
                hrm_ps_command_dtls command = db.hrm_ps_command_dtls.Find(id);
                int intCmdSeq = command.cmd_seq.Value;

                db.hrm_ps_command_dtls.Remove(command);
                db.SaveChanges();

                return RedirectToAction("Edit", new { id = intCmdSeq });
            }
            catch
            {
                return Edit(model.cmd_seq.Value);
            }
        }

        //
        // GET: /CommandPenalty/Delete/5

        public ActionResult Approve(int id = 0)
        {
            hrm_ps_commands item = db.hrm_ps_commands.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }

            //item.approved_date = common.CurrectDateTime(item.approved_date);
            return View(item);
        }

        //
        // POST: /CommandPenalty/Delete/5

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Details(int id, hrm_ps_commands model)
        {
            try
            {
                model = db.hrm_ps_commands.Find(id);

                using (var service = new CreateApproveService.ServiceClient())
                {
                    string strMessage = "";
                    int intEmpSeq = new SsMng().EmpSeqI;
                    service.CreateAbsenceTask("[คำสั่งบรรจุ]" + model.cmd_name
                        , intEmpSeq, model.cmd_seq, "P"
                        , 901, "A",intEmpSeq, out strMessage);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetCmdType(int id)
        {
            //using (GERPContext db = new GERPContext())
            //{
                var item = (from p in db.hrm_ps_command_types
                            where p.cmd_type_seq == id
                            select p).FirstOrDefault();
                return Json(item, JsonRequestBehavior.AllowGet);
            //}
        }

        public JsonResult GetDetail(int id)
        {
            //using (GERPContext db = new GERPContext())
            //{
                var item = (from p in db.hrm_ps_command_dtls
                            where p.cmd_dtls_seq == id
                            select p).FirstOrDefault();
                return Json(
                    new
                    {
                        cmd_dtls_seq = item.cmd_dtls_seq,
                        cmd_seq = item.cmd_seq,
                        emp_seq = item.emp_seq,
                        start_date = Common.GetDateString(item.start_date),
                        end_date = Common.GetDateString(item.end_date),
                        remarks = item.remarks,
                        rpt_remarks = item.rpt_remarks
                    }
                    , JsonRequestBehavior.AllowGet);
            //}
        }

        public ActionResult UpdateCommand(int id)
        {
            try
            {
                hrm_ps_commands model = db.hrm_ps_commands.Find(id);
                foreach (hrm_ps_command_dtls detail in model.col_hrm_ps_command_dtls)
                {
                    switch (model.penalty_seq)
                    {
                        case 1:
                            detail.hrm_ps_employees.emp_status_seq = 302;
                            break;
                        case 2:
                            detail.hrm_ps_employees.emp_status_seq = 905;
                            break;
                    }
                }

                model.record_status = "A";
                model.cmd_status = "R";

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return Details(id);
            }
        }
    }
}